package com.example.myshools.Activities;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.myshools.R;
import com.example.myshools.entity.Activities;
import com.example.myshools.entity.Img;
import com.example.myshools.entity.Orgnat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ActivityDetail extends AppCompatActivity {
    private ImageView headImg;
    private TextView name;
    private TextView lev;
    private TextView upDay;
    private TextView upTime;
    private TextView content;
    private NoScrollGridView gridView;
    private TextView list_biaoqian;
    private TextView num_views;
    private LinearLayout content_main;
    private Button to_finish;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        findView();
        setView();
    }


    private void findView() {
        headImg=findViewById(R.id.img_header);
        name=findViewById(R.id.name);
        lev=findViewById(R.id.lev);
        upDay=findViewById(R.id.up_day);
       upTime=findViewById(R.id.up_time);
        gridView=findViewById(R.id.grid_view);
        content=findViewById(R.id.content_text);
        list_biaoqian=findViewById(R.id.list_biao_qian);
        num_views=findViewById(R.id.num_liulan);
        content_main=findViewById(R.id.content_main);
        to_finish=findViewById(R.id.btn_return_setting);
    }
    private void setView() {
        to_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        Gson gson = new GsonBuilder().serializeNulls().create();
        String str = getIntent().getStringExtra("activity");
        Activities activities = gson.fromJson(str, Activities.class);
        String time = activities.getUpTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");//设置日期格式
        String currentTime = df.format(new Date());
        String a_day = time.substring(0, 8);
        int d = Integer.parseInt(a_day);

        String c_day = currentTime.substring(0, 8);
        int c = Integer.parseInt(c_day);
        if (a_day.equals(c_day)) {//表示是当天发的活动
            upDay.setText("今天");
        } else if (c - 1 == d) {
            upDay.setText("昨天");
        } else if (c - 2 == d) {
            upDay.setText("前天");
        } else {
            upDay.setText(a_day.substring(0, 4) + "." + a_day.substring(4, 6) + "." + a_day.substring(6, 8));
        }
        upTime.setText(time.substring(8, 10) + ":" + time.substring(10, 12));
        //发布者信息
        Orgnat orgnat = activities.getOrgnat();
        RequestOptions requestOptions = new RequestOptions()
                .centerCrop();

        Glide.with(this).load(orgnat.getHeadPath()).apply(requestOptions).into(headImg);
        name.setText(orgnat.getName());
        List<Img> imgList=activities.getImgPathList();
        if(imgList.size()>0) {
            GridViewItemImgAdapter gridViewItemImgAdapter = new GridViewItemImgAdapter(this, imgList, R.layout.grid_view_item);
            gridView.setAdapter(gridViewItemImgAdapter);
        }
        lev.setText(orgnat.getLevel());
        content.setText(activities.getContent());
        //标签
        String[] strings=activities.getLabel().split(",");
        String s="";
        for(int i=0;i<strings.length;i++){
            s=s+strings[i]+"\u3000";
        }
        list_biaoqian.setText(s);
        //点击量
        num_views.setText("点击量 "+activities.getViewNum());
    }

}
